import React from 'react';
import { bindActionCreators } from '@/redux';
import store from '@/store';
import * as actions from '@/store/actions/counter'
import { connect } from '@/react-redux';
import * as types from '@/store/action-types';

// const obj = bindActionCreators(actions, store.dispatch);


class Counter extends React.Component {
    constructor(props) {
        super(props);
    }
    render() {
        console.log(this.props);
        const { count, add, minus } = this.props;
        // const { count, dispatch } = this.props;
        return <>
            <div>{count}</div>
            <button onClick={add}>+</button>
            <button onClick={minus}>-</button>
            {/* <button onClick={() => dispatch({type: types.ADD})}>+</button>
            <button onClick={() => dispatch({type: types.MINUS})}>-</button> */}
        </>
    }
}


const mapStateToProps = (state) => state.counter;
// // 1 直接传一个对象
// const mapActionToProps = actions;
// // 2. 传一个函数，返回一个对象
const mapActionToProps = (dispatch) => ({
    add: () => dispatch({ type: types.ADD }),
    minus: () => dispatch({ type: types.MINUS })
})
// 3. 什么也不传，默认会给组件传一个dispatch
// const mapActionToProps = '';
// 链接组件
export default connect(mapStateToProps, mapActionToProps)(Counter);